﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace Print_UI
{
    public partial class FrmImportStudentProfile : CommonForm
    {
        public FrmImportStudentProfile()
        {

            InitializeComponent();

        }
        public SqlConnection _Cnn;
        private void btnImportListFee_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmImportSchoolFee fee = new FrmImportSchoolFee();
            fee.iDepartmentID = Convert.ToInt32(cboDepartment.EditValue);
            fee.iYearID = Convert.ToInt32(cboYear.EditValue);
            fee.iSemesterID = Convert.ToInt32(cboSemester.EditValue);
            fee.ShowDialog();
            RefreshModel();
            LoadData();

        }
        private void btnImportExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // FrmCheckNewDataInexcel frm = new FrmCheckNewDataInexcel();
            FrmImportScoreExcel excel = new FrmImportScoreExcel();
            excel.iYearschool = Convert.ToInt32(cboYear.EditValue);
            excel.iDepart = Convert.ToInt32(cboDepartment.EditValue);
            excel.iSemesterID = Convert.ToInt32(cboSemester.EditValue);
            excel.ShowDialog();
            LoadData();
            btnDelete.Enabled = true;

        }

        //Load  SChool Year
        private void LoadYear()
        {

            var Year = (from p in Model.CAT_Year
                        join k in Model.PRI_SemesterSetting on p.ID equals k.YearID
                        select new { p.ID, p.Name }).Distinct();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "----Chọn Năm Học----");
            foreach (var item in Year)
            {
                dic.Add(item.ID, item.Name);
            }

            cboYear.Properties.ValueMember = "Key";
            cboYear.Properties.DisplayMember = "Value";
            cboYear.Properties.DataSource = dic;
            cboYear.EditValue = -1;
        }
        //Load Semester
        private void LoadSemester()
        {

            Dictionary<int, string> dic = new Dictionary<int, string>();
            int iYear = Convert.ToInt32(cboYear.EditValue);
            var Semester = from obj in Model.PRI_SemesterSetting
                           where (obj.YearID == iYear)
                           select new
                           {
                               obj.CAT_Semester.ID,
                               obj.CAT_Semester.SemesterName
                           };
            dic.Add(-1, "----Chọn Học Kì----");
            foreach (var item in Semester)
            {
                dic.Add(item.ID, item.SemesterName);

            }
            cboSemester.Properties.ValueMember = "Key";
            cboSemester.Properties.DisplayMember = "Value";
            cboSemester.Properties.DataSource = dic;
            cboSemester.EditValue = -1;

        }
        // Load data Into GridView
        private void LoadData()
        {
            try
            {
                // groupBY and count Subjects of Student.
                int iddepart = Convert.ToInt32(cboDepartment.EditValue);
                int idsemester = Convert.ToInt32(cboSemester.EditValue);
                int idyear = Convert.ToInt32(cboYear.EditValue);

                gvdata.DataSource = Model.PRI_StudentInSemester.Where(c => c.DepartmentID == iddepart && c.SemesterID == idsemester && c.YearID == idyear).Select(c => new
                {
                    c.StudentID,
                    StudentCode = c.PRI_Student.Code,
                    StudentFName = c.PRI_Student.Firstname,
                    StudentLName = c.PRI_Student.Lastname,
                    SchoolFee = c.SchoolFee,
                    TotalMoneyDebt = c.SchoolFeeDebt,
                    Class = c.CAT_Class.Code,
                    TotalSubject = c.PRI_Student.PRI_StudentInSubject.Where(d => d.DepartmentID == iddepart && d.SemesterID == idsemester && d.YearID == idyear).Count()
                });
                gridColumn6.DisplayFormat.FormatString = "{0:#,###.##}";
                gridColumn6.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                gridColumn7.DisplayFormat.FormatString = "{0:#,###.##}";
                gridColumn7.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void CheckEnableItem()
        {
            int iddepart = Convert.ToInt32(cboDepartment.EditValue);
            int idsemester = Convert.ToInt32(cboSemester.EditValue);
            int idyear = Convert.ToInt32(cboYear.EditValue);
            if (iddepart != -1 && idsemester != -1 && idyear != -1)
            {
                var query = from Obj in Model.PRI_SemesterSetting.Where(c => c.YearID == idyear && c.SemesterID == idsemester)
                            select Obj;
                if (query.FirstOrDefault().IsUsed == true)
                {
                    if (gridView1.RowCount > 0)
                    {
                        btnImportExcel.Enabled = true;
                        btnImportListFee.Enabled = true;
                        btnDelete.Enabled = true;
                        btnupdateimfor.Enabled = true;
                        btnRefresh.Enabled = true;
                    }
                    else if (gridView1.RowCount == 0)
                    {
                        btnImportExcel.Enabled = true;
                        btnImportListFee.Enabled = false;
                        btnDelete.Enabled = false;
                        btnupdateimfor.Enabled = false;
                        btnRefresh.Enabled = false;
                    }

                }
                else
                {
                    btnImportExcel.Enabled = false;
                    btnImportListFee.Enabled = false;
                    btnDelete.Enabled = false;
                    btnupdateimfor.Enabled = false;
                    btnRefresh.Enabled = false;
                }
            }
            else if (idyear != -1 && idsemester == -1)
            {
                btnImportExcel.Enabled = false;
                btnImportListFee.Enabled = false;
                btnDelete.Enabled = false;
                btnupdateimfor.Enabled = false;
                btnRefresh.Enabled = false;
            }


        }
        private void LoadDepartment()
        {
            try
            {
                var departmentAdmin = from obj in Model.CAT_Department.OrderBy(c => c.Name)
                                      select obj;
                Dictionary<int, string> dicAdmin = new Dictionary<int, string>();
                dicAdmin.Add(-1, "----Chọn Khoa----");
                foreach (var item in departmentAdmin)
                {
                    dicAdmin.Add(item.ID, item.Name);

                }
                cboDepartment.Properties.ValueMember = "Key";
                cboDepartment.Properties.DisplayMember = "Value";
                cboDepartment.Properties.DataSource = dicAdmin;
                cboDepartment.EditValue = -1;

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        private void FrmImportStudentProfile_Load(object sender, EventArgs e)
        {

            btnImportExcel.Enabled = false;
            btnImportListFee.Enabled = false;
            btnDelete.Enabled = false;
            btnupdateimfor.Enabled = false;
            btnRefresh.Enabled = false;
            LoadYear();
            LoadDepartment();

        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                Point pt = view.GridControl.PointToClient(Control.MousePosition);
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);
                if (info.InRow || info.InRowCell)
                {
                    int iStudentID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("StudentID"));
                    FrmOverviewStudentScore frm = new FrmOverviewStudentScore();
                    frm.iStudentID = iStudentID;
                    frm.iYear = Convert.ToInt32(cboYear.EditValue);
                    frm.iDepartment = Convert.ToInt32(cboDepartment.EditValue);
                    frm.iSemester = Convert.ToInt32(cboSemester.EditValue);
                    frm.ShowDialog();
                    if (frm.DialogResult == DialogResult.OK)
                    {
                        LoadData();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cboDepartment_EditValueChanged(object sender, EventArgs e)
        {

            LoadData();
            CheckEnableItem();

        }
        private void btnCancle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Thoát Khỏi Giao Diện ?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void cboYear_EditValueChanged(object sender, EventArgs e)
        {

            LoadSemester();
            LoadDepartment();
            LoadData();
            CheckEnableItem();


        }
        private void cboSemester_EditValueChanged(object sender, EventArgs e)
        {
            LoadDepartment();
            LoadData();
            CheckEnableItem();

        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Hệ Thống Sẽ Xóa Tất Cả Dữ Liệu", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    _Cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString);
                    _Cnn.Open();

                    //delete data from SubjectSetting
                    SqlCommand cmmDeleteSubjectSetting = new SqlCommand("delete from PRI_SubjectSetting where DepartmentID = @Department and SemesterID =@SemesterID and YearID = @YearID", _Cnn);
                    cmmDeleteSubjectSetting.Parameters.Add("@Department", SqlDbType.Int).Value = Convert.ToInt32(cboDepartment.EditValue);
                    cmmDeleteSubjectSetting.Parameters.Add("@SemesterID", SqlDbType.Int).Value = Convert.ToInt32(cboSemester.EditValue);
                    cmmDeleteSubjectSetting.Parameters.Add("@YearID", SqlDbType.Int).Value = Convert.ToInt32(cboYear.EditValue);
                    cmmDeleteSubjectSetting.ExecuteNonQuery();
                    //delete data from StudentInsemester
                    SqlCommand cmmDeleteStudentSemester = new SqlCommand("delete from PRI_StudentInSemester where DepartmentID = @Department and SemesterID =@SemesterID and YearID = @YearID", _Cnn);
                    cmmDeleteStudentSemester.Parameters.Add("@Department", SqlDbType.Int).Value = Convert.ToInt32(cboDepartment.EditValue);
                    cmmDeleteStudentSemester.Parameters.Add("@SemesterID", SqlDbType.Int).Value = Convert.ToInt32(cboSemester.EditValue);
                    cmmDeleteStudentSemester.Parameters.Add("@YearID", SqlDbType.Int).Value = Convert.ToInt32(cboYear.EditValue);
                    cmmDeleteStudentSemester.ExecuteNonQuery();
                    //delete data from StudentInSubject
                    SqlCommand cmmDeleteStudentSubject = new SqlCommand("delete from PRI_StudentInSubject where DepartmentID = @Department and SemesterID =@SemesterID and YearID = @YearID", _Cnn);
                    cmmDeleteStudentSubject.Parameters.Add("@Department", SqlDbType.Int).Value = Convert.ToInt32(cboDepartment.EditValue);
                    cmmDeleteStudentSubject.Parameters.Add("@SemesterID", SqlDbType.Int).Value = Convert.ToInt32(cboSemester.EditValue);
                    cmmDeleteStudentSubject.Parameters.Add("@YearID", SqlDbType.Int).Value = Convert.ToInt32(cboYear.EditValue);
                    cmmDeleteStudentSubject.ExecuteNonQuery();

                    if (MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        LoadData();
                    }

                    _Cnn.Close();
                }

                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    _Cnn.Close();
                }
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshModel();
            LoadData();
        }

        private void btnupdateimfor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmUpdateExtraImforStudentDepartment frm = new FrmUpdateExtraImforStudentDepartment();
            frm.iDepartmentID = Convert.ToInt32(cboDepartment.EditValue);
            frm.iYearID = Convert.ToInt32(cboYear.EditValue);
            frm.iSemesterID = Convert.ToInt32(cboSemester.EditValue);
            frm.ShowDialog();
        }

    }

}
